using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using NUnit.Framework;
using Universe.JRun;

namespace Universe.CrossIO.Test
{
    [TestFixture]
    public class TestJCompiler
    {

        [Test]
        public void Test123()
        {
            JCompiler compiler = new JCompiler(
                @"System.out.print(123);");

            compiler.Compile();
        }

        [Test]
        public void TestHelloWorld()
        {
            foreach (JDescription jdk in JDescriptions.JDK)
            {
                Trace.WriteLine("HelloWorld on " + jdk);
                string helloWorld = "Hello, World!";
                JCompiler compiler = new JCompiler(
                    @"System.out.print(""" + helloWorld + @""");");

                compiler.JDK = jdk;
                JResponse response = compiler.Compile().Run();
                Assert.AreEqual("", response.Errors, "JDK " + jdk);
                Assert.AreEqual(helloWorld, response.Output, "JDK " + jdk);
            }
        }

        [Test]
        [ExpectedException(typeof(JCompilerException))]
        public void TestSyntaxError()
        {
            foreach (JDescription jdk in JDescriptions.JDK)
            {
                JCompiler compiler = new JCompiler(@"Invalid Syntax!");
                compiler.JDK = jdk;
                compiler.Compile();
            }
        }



        [Test]
        public void TestPefomance()
        {
            JCompiler compiler = new JCompiler(@"System.out.print(123); System.err.print(456);");

            JRunner jr = compiler.Compile();
            jr.Run();

            DateTime now = DateTime.Now;
            int count = 0;
            do
            {
                JResponse r = jr.Run();
                Assert.AreEqual("123", r.Output, "Check System.out");
                Assert.AreEqual("456", r.Errors, "Check System.err");
                count++;
            } while (DateTime.Now - now <= TimeSpan.FromSeconds(3));

            Trace.WriteLine(
                string.Format(
                    "Perfonamce: {0:f3} calls per second", 
                    count / (DateTime.Now - now).TotalSeconds));
        }

    }
}
